package one.stand.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.TicketApiGzhMapper;
import one.stand.model.ResultModel;
import one.stand.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Api(description = "奖券核销大屏")
@RestController
@RequestMapping("/ticket")
@Slf4j
public class TicketController {
    @Autowired
    private TicketApiGzhMapper ticketApiGzhMapper;

    @GetMapping("/count")
    public ResultModel<String> index() {
        TicketCountResponse response = ticketApiGzhMapper.selectTicketCount();
        return ResultModel.success(JSON.toJSONString(response, true));
    }

    @GetMapping("/index")
    public ResultModel<TicketIndexResponse> index(Integer pageSize) {
        PageHelper.startPage(1, ObjectUtil.defaultIfNull(pageSize, 20));//默认前20条
        List<TicketIndexResponse.Ticket> ticketIndexResponses = ticketApiGzhMapper.selectTicketList();
        PageResponse2<TicketIndexResponse.Ticket> page = ConvertUtils.page2(ticketIndexResponses, ticket -> {
            ticket.setTimeStr(DateUtil.format(DateUtil.HHmmss, ticket.getTime()));
            return ticket;
        });
//
//        PageResponse2<TicketIndexResponse.Ticket> page = new PageResponse2<>();
//        page.setList(Lists.newArrayList());
//        TicketIndexResponse.Ticket ticket;
//        for (int i = 0; i < ObjectUtil.defaultIfNull(pageSize, 20); i++) {
//            ticket = JSON.parseObject("{\n" +
//                    "\t\t\t\t\"time\": 1599466132000,\n" +
//                    "\t\t\t\t\"timeStr\": \"16:08:52\",\n" +
//                    "\t\t\t\t\"phone\": \"15236404486\",\n" +
//                    "\t\t\t\t\"num\": 5,\n" +
//                    "\t\t\t\t\"money\": 283207.00\n" +
//                    "\t\t\t}", TicketIndexResponse.Ticket.class);
//            ticket.setPhone(ticket.getTimeStr() + i);
//            page.getList().add(ticket);
//        }

        TicketIndexResponse response = TicketIndexResponse.builder().page(page).build();
        return ResultModel.success(response);
    }

    @Data
    @Builder
    public static class TicketIndexResponse {

        private PageResponse2<Ticket> page;

        @Data
        @Builder
        public static class Ticket {
            private Date time;
            private String timeStr;
            private String phone;
            private Integer num;
            private BigDecimal money;
        }
    }

    @Data
    @Builder
    public static class TicketCountResponse {
        private Integer 已经核对奖券数;
        private Integer 未核对奖券数;
        private Integer 已兑奖人数;
        private Integer 未兑奖人数;
    }
}
